<?php
/**
 * author      :chenwenwen
 * createTime  :2020-04-12 16:31
 * description :
 */

namespace app\api\validate;

use think\Validate;

class User extends Validate {
    protected $rule = [
        'username' => 'require',
        'phone_number' => 'require|verify_mobile',
        'code'=>'require|number|min:4',
        'type'=>'require|in:1,2',
        'sex'=>'require|in:0,1,2',
    ];
    protected $message = [
        'username' => '用户名必须',
        'phone_number' => '手机号必须',
        'code.require'=>'验证码必须',
        'code.number'=>'验证码必须为数字',
        'code.min'=>'验证码长度不能低于4',
        'type.require'=>'类型必须',
        'type.in'=>'登录类型数值错误',
        'sex.require'=>'性别必填',
        'sex.in'=>'性别数据错误'
    ];

    protected $scene = [
        'send_code' => ['phone_number'],
        'login'=>['phone_number','code','type'],
        'update_user'=>['username','sex']
    ];

    protected function verify_mobile($value, $rule, $data = []) {
        if (strlen($value) != 11) return '手机号长度必须是在11位';
        $value = mb_substr($value, -11);
        return preg_match('/^1[3456789]{1}\d{9}$/', $value) ? true : '手机号格式错误';
    }
}